// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.properties;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.warehouse.models.WarehouseNode;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

public class WarehouseViewPropsLabelProvider extends LabelProvider {

    // LabelProvider editorsNodeLabelProvider =
    // WarehousePlugin.getDefault().getEditorsPluginService().getEditorsNodeLabelProvider();

    @Override
    public Image getImage(Object element) {
        WarehouseNode warehouseNode = getWarehouseNode(element);
        if (warehouseNode != null) {
            ENodeCategoryName categoryName = warehouseNode.getCategoryName();
            switch (categoryName) {
            case PROCESS:
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_PROCESS);
            case DATABASE:
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_DATA);
            case BIN:
                return warehouseNode.getImage();
            }
        }
        // return editorsNodeLabelProvider.getImage(element);
        return null;
    }

    @Override
    public String getText(Object element) {
        WarehouseNode warehouseNode = getWarehouseNode(element);
        if (warehouseNode != null) {
            return warehouseNode.getLabel();
        }
        // return editorsNodeLabelProvider.getText(element);
        return null;
    }

    private WarehouseNode getWarehouseNode(Object element) {
        if (element instanceof WarehouseNode) {
            return (WarehouseNode) element;
        }
        if (element instanceof IStructuredSelection) {
            Object firstElement = ((IStructuredSelection) element).getFirstElement();
            return SectionFilter.getWarehouseNode(firstElement);
        }
        return null;
    }

}
